
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by zhoub on 2020/5/21.
 */
public class UdpServer {

    private int port=6680;
    private DatagramSocket socket;
    public UdpServer() throws IOException {
        socket =new DatagramSocket(port);
        System.out.println("UDP服务器已启动.....");
    }
    public String echo(String msg){
        return "server ："+msg;
    }
    public void service(){
        while(true){
            try{
                DatagramPacket packet =new DatagramPacket(new byte[2000],2000);
                socket.receive(packet);
//                String msg = new String(packet.getData(),0,packet.getLength(),"GB2312");
                System.out.println(packet.getAddress()+" : "+packet.getPort()+">"+packet.getLength());
//                packet.setData(echo(msg).getBytes("GB2312"));
                packet.setData(new byte[2000]);
                socket.send(packet);
            }catch(IOException e){
                System.out.println(e);
            }
        }
    }
    public static void main(String args[])throws IOException{
        new UdpServer().service();
    }
}
